package com.szqhyxj.youxiangju.biz.plan.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportActivity;
import com.szqhyxj.youxiangju.biz.creditReport.demonstration.DemonstrationActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot.EquipmentCardSlotActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard.HeadIdCardActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.onlineChannel.OnlineChannelListActivity;
import com.szqhyxj.youxiangju.biz.plan.RepaymentModel;
import com.szqhyxj.youxiangju.biz.plan.RepaymentPresenter;
import com.szqhyxj.youxiangju.biz.plan.adapter.BilledAdapter;
import com.szqhyxj.youxiangju.biz.plan.create.PlanCreateActivity;
import com.szqhyxj.youxiangju.biz.receipt.ReceiptActivity;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.ReceiptDetailActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.biz.web.WebExit;
import com.szqhyxj.youxiangju.sys.components.view.popview.ConsumePatternDialog;
import com.szqhyxj.youxiangju.sys.components.view.popview.OutlinePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by leung on 2018/11/4.
 */

public class BilledFragment extends BasePromptFragment implements BilledAdapter.IconClickListener, ConsumePatternDialog.ISharePlatformListener {


    Unbinder unbinder;
    static ArrayList<RepaymentModel.RepaymentEntity> billLists;
    @BindView(R.id.billedList)
    RecyclerView billedList;
    @BindColor(R.color.textcolor66)
    int grey;
    @BindView(R.id.none)
    LinearLayout none;
    RepaymentPresenter presenter;
    private BilledAdapter billedAdapter;
    private String cardNo;
    private RepaymentModel.RepaymentEntity repaymentEntity;

    public static BilledFragment newInstance(ArrayList<RepaymentModel.RepaymentEntity> list) {
        Bundle args = new Bundle();
        BilledFragment fragment = new BilledFragment();
        fragment.setArguments(args);
        billLists = list;
        return fragment;
    }

    public void update() {
        isgone();

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_billed;
    }

    @Override
    protected void start() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        presenter = new RepaymentPresenter();
        billedList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        billedList.setLayoutManager(new GridLayoutManager(getContext(), 1));
        billedAdapter = new BilledAdapter(billLists, this);
        billedList.setAdapter(billedAdapter);
        isgone();

        return rootView;
    }

    private void isgone() {
        if (billLists.size() == 0) {
            billedList.setVisibility(View.GONE);
            none.setVisibility(View.VISIBLE);
        } else {

            none.setVisibility(View.GONE);
            billedList.setVisibility(View.VISIBLE);
            Log.e("aaaaaaaaaa", billLists.size() + "");
            billedAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onClickItem(int i, List<RepaymentModel.RepaymentEntity> list) {

        RepaymentModel.RepaymentEntity repaymentEntity = list.get(i);

        if (repaymentEntity.getBankStates()==4||repaymentEntity.getBankStates()==1){

        }
        else {
        ConsumePatternDialog consumePatternDialog = new ConsumePatternDialog(getActivity());

        consumePatternDialog.setTitleDevice("设备还款");
        consumePatternDialog.setTitleOnline("快捷还款");
        consumePatternDialog.setcontentonline("费率：0.85%+单笔1元");
        consumePatternDialog.setnavigationbarTitle("还款方式");
        //设备是否激活 激活--是否在线   在线---是否报备   未在线---提示上线   未报备---去报备   报备---进行交易   未激活---去激活
        if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
            consumePatternDialog.setEquipmentContent("设备未激活，点击进行设备激活");
            consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
        } else {
            //已激活
            if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                //在线
                //判断是否报备
                if (repaymentEntity.getBankStates() == 0) {
                    //未报备
                    consumePatternDialog.setEquipmentContent("信用卡未报备，点击进行实名报备");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
                } else {
                    //已报备
                    consumePatternDialog.setEquipmentContent("费率：0.79%+单笔1.0元");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.white));
                }
            } else {
                //未在线
                consumePatternDialog.setEquipmentContent("您的设备已离线，无法正常进行交易");
                consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
            }
        }

            consumePatternDialog.setOnClickListener(new ConsumePatternDialog.ISharePlatformListener() {
                @Override
                public void onEquipmentRefund(View view) {
                    consumePatternDialog.dismiss();
                    if (repaymentEntity.getBankStates() == 0) {
                        if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {

                            HeadIdCardActivity.start(getActivity());
                            consumePatternDialog.dismiss();
                        } else {
                            DemonstrationActivity.start(getActivity());
                            consumePatternDialog.dismiss();
                        }
                    } else {
                        if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {

                            if (Integer.parseInt(repaymentEntity.getDetDays()) < 3) {
                                Toast.makeText(getActivity(), "还款日小于等于3天不能进行制定计划！", Toast.LENGTH_LONG).show();
                                return;
                            } else {
                                String cardNo = repaymentEntity.getBankCardNo();
                           /* PlanCreateActivity.start(getActivity(), repaymentEntity.getFixedAmount(), repaymentEntity.getBtName() + "(" + cardNo.substring(cardNo.length() - 4, cardNo.length()) + ")", repaymentEntity.getBankCardNo(), Integer.parseInt(repaymentEntity.getBillingDate()), Integer.parseInt(repaymentEntity.getDueDate()), repaymentEntity.getAreainfo());*/
                                String title = repaymentEntity.getBtName() + "(" + cardNo.substring(cardNo.length() - 4, cardNo.length()) + ")";
                                WebActivity.startToH5(getActivity(), "createplan?type=offline&&billDate=" + repaymentEntity.getBillingDate() + "&&repayDate=" + repaymentEntity.getDueDate() + "&&title=" + title + "&&channelKey=jiaLian_pos_channel" + "&&channelName=" + "" + "&&merchantArea=" + repaymentEntity.getAreainfo() + "&&cardNo=" + cardNo, WebExit.PLAN_LIST.getCode());

                            }
                        } else {
                            OutlinePopView outlinePopView = new OutlinePopView(getActivity());
                            outlinePopView.setOnClickListener(new OutlinePopView.OnClickListener() {
                                @Override
                                public void onClickConfirm(View v) {
                                    EquipmentCardSlotActivity.start(getActivity());
                                }
                            });

                            outlinePopView.show();
                        }
                    }
                }

                @Override
                public void onShortcutRefund(View view) {
                    consumePatternDialog.dismiss();
                    OnlineChannelListActivity.start(getActivity(), repaymentEntity.getCardId(), repaymentEntity.getBtName(), repaymentEntity.getBankCardNo(), repaymentEntity.getFixedAmount(), "1", repaymentEntity.getCardPhone(), repaymentEntity.getBillingDate(), repaymentEntity.getDueDate(), repaymentEntity.getAreainfo());
                    consumePatternDialog.dismiss();

                }
            });

            consumePatternDialog.show();
        }



    }

    @Override
    public void onEquipmentRefund(View view) {

    }

    @Override
    public void onShortcutRefund(View view) {

    }


    //============================================还款线上线下选择=====================================
//    @Override
//    public void onClickItem(int i, List<RepaymentModel.RepaymentEntity> list) {
//        repaymentEntity = list.get(i);
//
//        String equipmentContent  ;
//        ConsumePatternDialog dialog = new ConsumePatternDialog(getContext());
//        RepaymentModel.RepaymentEntity repaymentEntity = list.get(i);
//        if (repaymentEntity.getBankStates() == 0) {     //信用卡是否报备  0未报备  1计划执行中  2制定计划
//            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {   //设备是否激活  0未激活 1设备已激活
//                equipmentContent = "设备未激活，点击进行设备激活";
//            } else {
//                equipmentContent = "信用卡未报备,点击进行实名报备";
//            }
//        }else {
//            equipmentContent = "费率：0.85%+单笔0.5元";
//            dialog.setEquipmentContentColor(grey);
//        }
//
//        dialog.setEquipmentContent(equipmentContent);
//        dialog.setISharePlatformListener(this);
//        dialog.show();
//
//    }
//
//    @Override
//    public void onEquipmentRefund(View view){
//        if (repaymentEntity.getBankStates() == 0) {     //信用卡是否报备  0未报备  1计划执行中  2制定计划
//            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {   //设备是否激活  0未激活 1设备已激活
//                HeadIdCardActivity.start(getActivity());
//
//            } else {
//                CreditReportActivity.start(getActivity(), repaymentEntity.getCardId());
//            }
//        }
//
//        if (repaymentEntity.getBankStates() == 1) {
//
//        }
//
//        if (repaymentEntity.getBankStates() == 2) {
//            if (Integer.parseInt(repaymentEntity.getDetDays()) <= 3) {
//                Toast.makeText(getActivity(), "还款日小于等于3天不能进行制定计划！", Toast.LENGTH_LONG).show();
//                return;
//            } else {
//                cardNo = repaymentEntity.getBankCardNo();
//                PlanCreateActivity.start(getActivity(), repaymentEntity.getFixedAmount(), repaymentEntity.getBtName() + "(" + cardNo.substring(cardNo.length() - 4, cardNo.length()) + ")", repaymentEntity.getBankCardNo(), Integer.parseInt(repaymentEntity.getBillingDate()), Integer.parseInt(repaymentEntity.getDueDate()), repaymentEntity.getAreainfo());
//
//            }
//
//        }
//    }
//
//    @Override
//    public void onShortcutRefund(View view){
//        OnlineChannelListActivity.start(getActivity(),cardNo);
//    }
}
